import axios from "axios";
import { message as messageBox } from 'antd';
import store from '../store/store';
import { tansParams } from '../utils/bigwolf'
import { getToken } from '../utils/auth'

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建 Axios 实例
const api2 = axios.create({
  baseURL: "/api2",
  timeout: 50000,
});

// 请求拦截器
api2.interceptors.request.use(
  (config) => {
    config.headers['Authorization'] = 'Bearer pat_EwknijsXgUVMkQWHrfYT5srtFLwCNO365aCb6oymU4txzgYJ5yNf5oOQsDPOm2Ha';
      // get请求映射params参数
    if (config.method === 'get' && config.params) {
      let url = config.url + '?' + tansParams(config.params);
      url = url.slice(0, -1);
      config.params = {};
      config.url = url;
    }
    return config;
  },
  (error) => {
    let { message } = error;
    messageBox.error(message, 5);

    return Promise.reject(error);
  }
);

  
  // 响应拦截器
  api2.interceptors.response.use(
    response => {
      // 对响应数据做点什么
      //console.log('===response===',response);
        const code =response.status;
        const msg = response.statusText; 
        //console.log('===response===',code);
        if(code === 403) {
          const message = '登录超时，请重新登录';
          messageBox.error(message, 5);
          return Promise.reject({ // 修改这里
            message,
            statusCode: code
          });
        }
        if(code !== 200) {
        //ElMessage({ message: msg, type: 'error' , duration: 5 * 1000})
        messageBox.error(msg, 5);
        //return Promise.reject(new Error(msg));
        return Promise.reject({ // 修改这里
          message: msg,
          statusCode: code
        });
      } else {
        return  Promise.resolve(response.data);
      }
    },
    error => {
      // 对响应错误做点什么
      //console.log('==response=error===',error);
      let { message } = error;
      messageBox.error(message, 5);
      //location.href = '/login';
      return Promise.reject(error);
    }
  );
export default api2;